package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.BaseBean;
import com.magicborrow.utils.MD5;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.ViewPressedUtil;
import com.magicborrow.utils.VolleyTool;

import java.util.HashMap;

/**
 * Created by ghost on 16/6/22.
 */
public class PasswordActivity extends BaseActivity {


    private ImageView ivBack;
    private TextView tvcenter;
    private TextView confirm;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);

        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvcenter = (TextView) findViewById(R.id.tvcenter);
        confirm = (TextView) findViewById(R.id.confirm);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        confirm.setOnTouchListener(ViewPressedUtil.TouchPress);
        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!checkPassword())return;
                HashMap<String,String> params = new HashMap<String, String>();
                params.put("acc_token", UserTools.getUser(PasswordActivity.this).getAcc_token());
                params.put("password", MD5.getMD5(MD5.getMD5(password)));
                VolleyTool.get(Constants.URL_POST_CHECK_PASSWORD, params, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        BaseBean bean = (BaseBean) t;
                        if(bean.getCode()>=0){
                            Intent intent = new Intent(PasswordActivity.this,CheckPhoneActivity.class);
                            startActivity(intent);
                            finish();

                        }else {
                            showShortMsg(bean.getMessage());
                        }

                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {

                    }
                },0, BaseBean.class);

            }
        });
    }

    private EditText getEtContent() {
        return (EditText) findViewById(R.id.et_content);
    }
    private boolean checkPassword(){
        password = getEtContent().getText().toString();
        if("".equals(password)){
            Toast.makeText(this,"密码不能为空",Toast.LENGTH_SHORT).show();
            return false;
        }
        if(password.length() < 6){
            Toast.makeText(this,"密码不能低于6位数",Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }


}
